﻿using Admin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Admin.Controllers
{
    public class ZiXunController : ApiController
    {
        // GET: api/ZiXun
        [HttpGet]
        public IHttpActionResult GetList(string UserName, int page = 1, int limit = 5)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var datalist = db.UserChecks.AsEnumerable();
                if (!string.IsNullOrEmpty(UserName))
                {
                    datalist = datalist.Where(p => p.UserName.Contains(UserName));
                }
                var count = datalist.Count();
                var list = datalist.Select(p => new {
                    Id = p.Id,
                    UserId = p.UserId,
                    UserName = p.UserName,
                    CheckinTime = p.CheckinTime.ToString(),
                    CheckState = p.CheckState,
                    CheckOutTime = p.CheckOutTime.ToString(),
                    IsCancel = p.IsCancel,
                    Weight = p.Weight
                }).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { count = count, data = list });
            }
        }
        [HttpPost]
        public IHttpActionResult Add([FromBody] UserChecks user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                //查询用户
                var uslist = db.Users.SingleOrDefault(p => p.Id == user.UserId);
                //获取当前时间
                string nowtime = DateTime.Now.ToString("yyyy-MM-dd");
                DateTime time = DateTime.Parse(nowtime);
                //登录的用户id查询签到表中的用户
                var datelist = db.UserChecks.Where(p => p.UserId == uslist.Id).ToList();
                //判断是否在当天零时签到
                var date = datelist.SingleOrDefault(p => p.CheckinTime >= time);
                if (date == null)//说明此员工今天没有签到，进行签到处理
                {
                    var checklist = new UserChecks()
                    {
                        UserId = uslist.Id,
                        UserName = uslist.LoginName,
                        CheckinTime = DateTime.Now,
                        CheckState = "已签到",
                        CheckOutTime = null,
                        IsCancel = "未签退"
                    };
                    db.UserChecks.Add(checklist);
                    int n = db.SaveChanges();
                    if (n > 0)

                    {
                        return Json(new { code = 0, msg = "上班打卡成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "上班打开失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "今天已完成上班打卡，请勿重复操作，谢谢配合！！！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Add2([FromBody] UserChecks user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                //查询用户
                var uslist = db.Users.SingleOrDefault(p => p.Id == user.UserId);
                //获取当前时间
                string nowtime = DateTime.Now.ToString("yyyy-MM-dd");
                DateTime time = DateTime.Parse(nowtime);
                //登录的用户id查询签到表中的用户
                var datelist = db.UserChecks.Where(p => p.UserId == uslist.Id).ToList();
                //签到签退时间对比
                var checkin = datelist.SingleOrDefault(p => p.CheckinTime >= time);
                var checkout = datelist.SingleOrDefault(p => p.CheckOutTime >= time);
                if (checkin == null)
                {
                    return Json(new { code = -1, msg = "请进行上班打卡！！！" });
                }
                else
                {
                    if (checkout == null)
                    {
                        if (DateTime.Now - checkin.CheckinTime >= TimeSpan.Parse("00:10:00"))
                        {
                            checkin.IsCancel = "已签退";
                            checkin.CheckOutTime = DateTime.Now;
                            int m = db.SaveChanges();
                            if (m > 0)
                            {
                                return Json(new { code = 0, msg = "下班打卡成功！" });
                            }
                            else
                            {
                                return Json(new { code = -1, msg = "下班打卡失败！" });
                            }
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "上班打卡时间未到十分钟，不能进行打卡签退" });
                        }
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "今天已完成下班打卡，明天欢迎你的到来！" });
                    }
                }
            }
        }
        [HttpPost]
        public IHttpActionResult Edit([FromBody] UserChecks user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.UserChecks.FirstOrDefault(p => p.Id == user.Id);
                list.Weight = user.Weight;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功！" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败！" });
                }
            }
        }
        [HttpPost]
        public IHttpActionResult GetRole([FromBody]Users user)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var list = db.UserRoles.Where(p => p.UserId == user.Id).ToList();
                var data = db.Roles.SingleOrDefault(p => p.Name == "咨询师");
                foreach (var item in list)
                {
                    if (item.RoleId == data.Id)
                    {
                        return Json(new { code = 0, IsShow = true });
                    }
                    else
                    {
                        continue;
                    }
                }
                return Json(new { code = -1, IsShow = false });
            }
        }
    }
}
